//
//  YPMineViewController.m
//  project
//
//  Created by 姚敦鹏 on 2018/10/25.
//  Copyright © 2018 rocHome. All rights reserved.
//

#import "YPMineViewController.h"
#import "YPMineTableViewCell.h"
#import <UShareUI/UShareUI.h>
#import "YPMineSectionView.h"

@interface YPMineViewController ()

@end

@implementation YPMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.hideCurrentNavBar = YES;
    [self MnSubViewInit];
    [self requestDataSource];
}

- (void)MnSubViewInit {
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YPMineTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([YPMineTableViewCell class])];

    
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.mas_equalTo(self.view);
    }];
}

- (void)requestDataSource {
    
    [YPProgressHUD showDIYLoading];
    
    NSArray *arr = @[@{@"iconName":@"refresh_1",@"title":@"微信"},
                     @{@"iconName":@"refresh_2",@"title":@"QQ"},
                     @{@"iconName":@"refresh_3",@"title":@"新浪微博"}
                     ];
    
    for (NSInteger i = 0; i < 4; i++) {
        
        YPMineSectionModel *model = [YPMineSectionModel modelWithTitle:@"分享" withContents:arr withOpen:i == 0];
        [self.dataList addObject:model];
    }
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [YPProgressHUD dismissHUD];
        [self.tableView pullRefreshEnd];
        self.tableView.emptyStatus = YPPageEmptyStatusHidden;
        [self.tableView reloadData];
    });
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataList.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    YPMineSectionModel *model = [self.dataList objectAtIndex:section];
    if (model.isOpen) {
        return model.contents.count;
    }
    return 0;
}

#pragma mark -- TableView Delegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YPMineSectionModel *model = [self.dataList objectAtIndex:indexPath.section];
    YPMineTableViewCell *cell = [YPCellFactory createCellWithModel:[model.contents objectAtIndex:indexPath.row] tableView:tableView indexPath:indexPath];
    
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    YPMineSectionModel *model = [self.dataList objectAtIndex:section];
    YPMineSectionView *headerView = [YPMineSectionView viewFromXIB];
    headerView.titleLabel.text    = model.title;
    headerView.arrowButton.selected = model.isOpen;
    headerView.openBlock = ^{
        model.open = !model.isOpen;
        [tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:(UITableViewRowAnimationNone)];
    };
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 60;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [super tableView:tableView didSelectRowAtIndexPath:indexPath];
    
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
        [self shareImageAndTextToPlatformType:platformType];
    }];
}

- (void)shareImageAndTextToPlatformType:(UMSocialPlatformType)platformType
{
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    
    //设置文本
    messageObject.text = @"社会化组件UShare将各大社交平台接入您的应用，快速武装App。";
    
    //创建图片内容对象
    UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
    //如果有缩略图，则设置缩略图
    shareObject.thumbImage = [UIImage imageNamed:@"refresh_1"];
    [shareObject setShareImage:[UIImage imageNamed:@"refresh_1"]];
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            NSLog(@"************Share fail with error %@*********",error);
        }else{
            NSLog(@"response data is %@",data);
        }
    }];
}


@end
